<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="hightQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--
                private String name;
                private String sn;
                private Integer state;
                private Employee manager;
                private Dictionary parent;
        -->
		<!--列表-->
		<el-table :data="dictionaryDetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="字典明细名称" >
			</el-table-column>
			<el-table-column prop="types_id" label="字典明细编号" >
			</el-table-column>


			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>
		<!--
                private String name;
                private String sn;
                private Integer state;
                private Employee manager;
                private Dictionary parent;
        -->
		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
				<el-form-item label="字典明细名称" prop="name" >
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典明细编号" prop="types_id">
					<el-input v-model="editForm.types_id" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    //import { getDictionaryListPage, removeDictionary, batchRemoveDictionary, editDictionary, addDictionary } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    keyword: ''
                },
                dictionaryDetails: [],
                totals: 0,
                pageSize: 5,
                currentPage:1,
                listLoading: true,
                sels: [],//列表选中列
                title:'',

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入字典明细名称', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入字典明细编号', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    name: '',
                    sn: "",
                },
            }
        },
        methods: {


            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDictionaryDetails();
            },
            hightQuery(){
                this.currentPage=1;
                this.getDictionaryDetails();
            },
            //获取用户列表
            getDictionaryDetails() {
                let para = {//传到后台的数据：
                    currentPage: this.currentPage,//当前页
                    pageSize: this.pageSize,//pageSize
                    keyword: this.filters.keyword
                };
                this.listLoading = true;

                //NProgress.start();  全局的使用axios 发送  跨域请求
                this.$http.post("/dictionaryDetail",para).then(res=>{//成功
                    this.dictionaryDetails = res.data.data;
                    this.totals = res.data.totalCount;
                    this.listLoading = false;/*忙态框*/
                }).catch(res=>{//失败

                })

                /*getDictionaryListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.dictionarys = res.data.dictionarys;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;//忙态框
                    //NProgress.start();
                    let para = "/dictionaryDetail/"+row.id;
                    this.$http.delete(para).then(res=>{//请求成功ajaxResult{success:true,msg:"操作成功"}
                        this.listLoading = false;//关闭忙态框
                        if(res.data.success){
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if(this.currentPage>1 && this.totals%this.pageSize==1){
                                this.currentPage=this.currentPage-1
                            }
                            this.getDictionaryDetails();
                        }else{
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                        }

                    }).catch(res=>{//系统错误
                        alert("系统错误")
                    })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {/*显示编辑的模态框*/
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);//回显
                this.title="编辑"


            },
            //显示新增界面
            handleAdd: function () {
                this.editFormVisible = true;
                this.editForm = {/*清空表单*/
                    name: '',
                    types_id: "",
                }
                this.title="新增"

            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {//验证表单
                    if (valid) {//验证通过
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {//确认就进来
                            this.editLoading = true;//显示忙态框

                            let para = Object.assign({}, this.editForm);//复制

                            this.$http.put("/dictionaryDetail",para).then(res=>{
                                this.editLoading = false;/*关闭忙态框*/
                                if(res.data.success){//成功
                                    this.$message({/*提示框*/
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();/*重置表单*/
                                    this.editFormVisible = false;/*关闭编辑模态框*/
                                    this.getDictionaryDetails();/*重新加载数据*/
                                }else{//失败
                                    this.$message({/*提示框*/
                                        message: '提交失败',
                                        type: 'error'
                                    });
                                }
                            }).catch(res=>{
                                alert("系统错误")
                            })


                        })
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            addDictionaryDetail(para).then((res) => {
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getDictionaryDetails();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },

        mounted() {
            this.getDictionaryDetails();
        }
    }

    }

</script>

<style scoped>

</style>